package com.lovo.bbs.servlet.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovo.bbs.bo.ForumBo;
import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.bo.PostBo;
import com.lovo.bbs.bo.TopicBo;
import com.lovo.bbs.bo.UserInfoBo;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.ForumVo;
import com.lovo.bbs.vo.UserInfoVo;

/**
 * 管理用户更新Servlet
 * 
 * @author tiancen2001
 * 
 */
public class UserUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = -1822041040057695899L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		// 操作
		int opType = 0;
		if (request.getParameter("opType") != null)
			opType = Integer.parseInt(request.getParameter("opType"));

		// 请求页
		String queryPages = (String) request.getParameter("queryPage");
		if (queryPages == null || "".equals(queryPages)
				|| "0".equals(queryPages)) {
			queryPages = "1";
		}
		int queryPage = Integer.parseInt(queryPages);

		//用户属性
		int userid = Integer.parseInt(request.getParameter("userid"));// 用户ID
		int topicnum = Integer.parseInt(request.getParameter("postnum"));// 用户主题数
		String email = request.getParameter("email");// e-Mail
		int score = Integer.parseInt(request.getParameter("credits"));// 积分

		//封装
		UserInfoVo vo = new UserInfoVo();
		vo.setUserId(userid);
		vo.setTopicNum(topicnum);
		vo.setEmail(email);
		vo.setScore(score);

		//更新
		int dataChanged = 0;
		ServletContext context = request.getSession().getServletContext();
	    ForumBo forumBo = new ForumBo();
	    ForumStatBo forumStatBo = new ForumStatBo();
	    TopicBo topicBo = new TopicBo();
	    UserInfoBo userInfoBo = new UserInfoBo();
		if (opType == 2) {// 更新用户
			dataChanged = userInfoBo.updateUser(vo, UserInfoBo.OPER_OF_UPDATE);
		} else if (opType == 3) {// 删除用户
			//删除用户(0)
			userInfoBo.delUser(userid);
			
			//删除该用户的主题,返回主题所属的论坛(1)
			HashSet<Integer> forumIDs = topicBo.delTopics(userid);
			
			//删除该用户的回帖,返回回帖所属的主题(2)
			HashSet<Integer> topicIDs = new PostBo().delPostByUserID(userid);

			//更新主题的统计数据,同时记下主题所属的论坛(3)
			Iterator<Integer> it1  =  topicIDs.iterator();
			while(it1.hasNext()){
				int topicid = it1.next();

				topicBo.calcPostNum(topicid);//更新主题回帖数
				
				topicBo.setLastPost(topicid);//更新主题最后回帖
				int forumid=topicBo.getForumID(topicid);
				forumIDs.add(forumid);
			}
			
			//更新论坛统计数据(4)
			Iterator<Integer> it2  =  forumIDs.iterator();
            while(it2.hasNext()){
            	int forumid = it2.next();
            	forumBo.calcTopicNum(forumid);//更新论坛主题数
            	forumBo.calcPostNum(forumid);//更新论坛回帖数
            	forumBo.calcLastTopic(forumid);//更新论坛最后主题
            }
            forumBo.calcTodayTopicNum();//更新论坛今日主题数
            
			// 取得所有论坛列表(5)
			ArrayList<ForumVo> forumList = forumBo.getAllForum();
			// 更新论坛列表
			context.setAttribute("forumList", forumList);
			// 更新论坛MAP
			context.setAttribute("forumMap", forumBo.getForumMap(forumList));
			
			// 更新网站杂项信息数据库(6)
			dataChanged = forumStatBo.reset();
			// 更新内存网站杂项信息(7)
			ForumStatVo forumInfo = forumStatBo.getForumInfo();
			context.setAttribute("forumInfo", forumInfo);
		}
		
		if (dataChanged == 1) {
			resp.sendRedirect("userManageQueryPage.jsp?queryPage=" + queryPage);
		}
	}
}
